package com.qxsx.midi.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.main.MainActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.AppManager;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Joney on 2015/9/1.
 */
public class RegisterActivity extends Activity implements View.OnClickListener {
    private CustomProgress customProgress;

    private LinearLayout ll_back;//返回
    private Button btn_register, btn_send;
    private EditText et_phone, et_yanzheng, et_password, et_password_again;//电话，验证，密码，再次密码

    private final static int WHAT_DAOJISHI = 1;

    // 可以验证
    boolean canChecking = true;
    // 验证用的Timer
    Timer timer;
    // 间隔时间
    final int spacing = 60;
    // 剩余时间
    int lastTime;

    String yanzhengma = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        findById();
        AppManager.getAppManager().addActivity(this);

    }

    private void findById() {
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);

        btn_register = (Button) findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this);

        btn_send = (Button) findViewById(R.id.btn_send);
        btn_send.setOnClickListener(this);

        et_phone = (EditText) findViewById(R.id.et_phone);

        et_yanzheng = (EditText) findViewById(R.id.et_yanzheng);

        et_password = (EditText) findViewById(R.id.et_password);

        et_password_again = (EditText) findViewById(R.id.et_password_again);


    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case WHAT_DAOJISHI:
                    if (canChecking)    // 如果可以获取验证码
                    {
                        canChecking = false;
                        timer = new Timer();
                        lastTime = spacing;
                        final String oldtxt = btn_send.getText().toString();
                        btn_send.setText(lastTime + "S");
                        btn_send.setTextColor(getResources().getColor(R.color.theme_green));
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                if (lastTime == 0) // 时间到了
                                {
                                    RegisterActivity.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            btn_send.setClickable(true);
                                            btn_send.setBackgroundResource(R.mipmap.yanzheng);
                                            btn_send.setText(oldtxt);
                                            btn_send.setTextColor(getResources().getColor(R.color.theme_green));
                                            timer.cancel();
                                            timer = null;
                                            canChecking = true;
                                        }
                                    });

                                } else {

                                    RegisterActivity.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            btn_send.setClickable(false);
                                            lastTime--;
                                            btn_send.setText(lastTime + "S");
                                        }
                                    });

                                }
                            }
                        }, 1000, 1000);
                    } else            // 不可以获取验证码
                    {
                        return;
                    }


                    break;
            }
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
                AppManager.getAppManager().finishActivity(RegisterActivity.class);
                break;

            case R.id.btn_register:
                if (TextUtils.isEmpty(et_phone.getText().toString()) || !Tools.isMobileNO(et_phone.getText().toString().trim())) {
                    Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                } else {
                    if (TextUtils.isEmpty(et_yanzheng.getText().toString())) {
                        Toast.makeText(RegisterActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    } else {
                        /**
                         * 在这里判断验证码是否正确
                         */
                        if (et_yanzheng.getText().toString().equals(yanzhengma)) {

                            if (TextUtils.isEmpty(et_password.getText().toString())) {
                                Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                            } else {
                                if (TextUtils.isEmpty(et_password_again.getText().toString())) {
                                    Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();

                                } else {
                                    if (!et_password_again.getText().toString().equals(et_password.getText().toString())) {
                                        Toast.makeText(RegisterActivity.this, "请两次输入相同的密码", Toast.LENGTH_SHORT).show();

                                    } else {
                                        customProgress = CustomProgress.show(RegisterActivity.this, "注册中....", true, null);
                                        JSONArray array = new JSONArray();
                                        JSONObject object = new JSONObject();
                                        String client_id = (String) SPUtils.get(AppContext.getContext(), "client_id","");
                                        try {
                                            object.put("login_name", et_phone.getText().toString());
                                            object.put("pwd", et_password.getText().toString());
                                            object.put("sure_pwd", et_password.getText().toString());
                                            object.put("code", yanzhengma);
                                            object.put("cid", client_id);
                                            array.put(object);


                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                        MyNet.doPostJsonArray("102", array, new callBackJsonArray() {
                                            @Override
                                            public void run(Object code, Object message, JSONArray array) {
                                                if (code.toString().equals("425")) {
                                                    Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                                } else if (array == null) {
                                                    Log.w("array", array + "");
                                                    return;
                                                } else {
                                                    try {
                                                        JSONObject object1 = array.getJSONObject(0);
                                                        String user_id = object1.getString("user_id");
                                                        SPUtils.put(AppContext.getContext(),"user_id",user_id);
                                                        SPUtils.put(AppContext.getContext(), "login_name", et_phone.getText().toString());
                                                        SPUtils.put(AppContext.getContext(), "login_password", et_password.getText().toString());
                                                        Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                                        //注册成功
                                                        Intent intent = new Intent(AppContext.getContext(), InformationActivity.class);
                                                        AppManager.getAppManager().finishActivity();
                                                        startActivity(intent);
                                                        customProgress.dismiss();
                                                        finish();


                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }

                                                }
                                            }
                                        });


                                    }


                                }


                            }


                        } else {
                            Toast.makeText(RegisterActivity.this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();

                        }


                    }
                }


                break;

            case R.id.btn_send:
                if (Tools.isMobileNO(et_phone.getText().toString().trim())) {
                    /**
                     * 获取验证码
                     */
                    JSONArray array = new JSONArray();
                    JSONObject object = new JSONObject();
                    try {
                        object.put("phone", et_phone.getText().toString());
                        array.put(object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    MyNet.doPostJsonArray("107", array, new callBackJsonArray() {
                                @Override
                                public void run(Object code, Object message, JSONArray array) {
                                    if (array == null) {
                                        Log.w("array", array + "");
                                        return;
                                    } else {
                                        try {
                                            JSONObject object1 = array.getJSONObject(0);
                                            Log.w("array", array + "");
                                            yanzhengma = object1.getString("validate_num");
                                            // Toast.makeText(RegisterActivity.this, yanzhengma, Toast.LENGTH_SHORT).show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }

                    );


                    handler.sendEmptyMessage(WHAT_DAOJISHI);

                } else {
                    Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();

                }
                break;

            default:

                break;
        }
    }
}
